package cms.utils;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Assert;

/**
 * 让普通的java类获取spring 的bean配置
 * 此类可以取得Spring的上下文
 * 
 * OrderService orderService = (OrderService)SpringConfigTool.getContext().getBean("orderServiceBean");
 *
 */
public class SpringContextHolder implements ApplicationContextAware{
	private static ApplicationContext applicationContext = null;

	private static Log logger = LogFactory.getLog(SpringContextHolder.class);

	/**
	 * 实现ApplicationContextAware接口, 注入Context到静态变量中.
	 */
	public void setApplicationContext(ApplicationContext applicationContext) {
		logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext);

		if (SpringContextHolder.applicationContext != null) {
			logger.warn("SpringContextUtil中的ApplicationContext被覆盖, 原有ApplicationContext为:"
					+ SpringContextHolder.applicationContext);
		}
		SpringContextHolder.applicationContext = applicationContext; //NOSONAR
	}

	/**
	 * 采用classpath创建context
	 */
	public void setApplicationContext(String... configLocations) {
		Assert.noNullElements(configLocations, "configLocations 不能为空.");

		logger.debug("注入ApplicationContext到SpringContextUtil:" + applicationContext);

		if (SpringContextHolder.applicationContext != null) {
			logger.warn("SpringContextUtil中的ApplicationContext被覆盖, 原有ApplicationContext为:"
					+ SpringContextHolder.applicationContext);
		}

		SpringContextHolder.applicationContext = new ClassPathXmlApplicationContext(configLocations);
	}

	/**
	 * 实现DisposableBean接口,在Context关闭时清理静态变量.
	 */
	public void destroy() throws Exception {
		SpringContextHolder.clear();
	}

	/**
	 * 取得存储在静态变量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext(){
		return applicationContext;
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		assertContext();
		return (T) applicationContext.getBean(name);
	}

	public static <T> T getBean(String name,Class<T> requiredType) {
		assertContext();
		return (T) applicationContext.getBean(name,requiredType);
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> requiredType) {
		assertContext();
		return (T) applicationContext.getBean(requiredType);
	}



	/**
	 * 清除SpringContextUtil中的ApplicationContext为Null.
	 */
	public static void clear() {
		logger.debug("清除SpringContextUtil中的ApplicationContext:" + applicationContext);
		applicationContext = null;
	}

	/**
	 * 检查ApplicationContext不为空.
	 */
	private static void assertContext() {
		getApplicationContext();
		if (applicationContext == null) {
			throw new IllegalStateException("applicaitonContext为空.");
		}
	}

}
